package com.bignerdranch.android.furnitureapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bignerdranch.android.furnitureapplication.adapter.NewsInfoAdapter;
import com.bignerdranch.android.furnitureapplication.entity.NewsInfoBean;
import com.bignerdranch.android.furnitureapplication.net.RetrofitHelper;
import com.bignerdranch.android.furnitureapplication.widgets.AutoScrollViewPager;
import com.bignerdranch.android.furnitureapplication.widgets.BaseViewPagerAdapter;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainFragment extends BaseFragment implements View.OnClickListener {

    private AutoScrollViewPager mScrollViewPager;
    private RecyclerView mRecyclerView;
    private List<NewsInfoBean> dataList;
    private LinearLayout ll1;
    private LinearLayout ll2;
    private LinearLayout ll3;
    private LinearLayout ll4;

    private String[] paths = {
            "http://cmf.328f.cn/UploadFile/20180122174524_5140.jpg",
            "http://cmf.328f.cn/UploadFile/00.jpg",
            "http://cmf.328f.cn/UploadFile/11.jpg",
            "http://cmf.328f.cn/UploadFile/33.jpg",
            "http://cmf.328f.cn/UploadFile/55.jpg"};

    public static MainFragment newInstance() {
        return new MainFragment();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_main;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(getLayoutResId(), container, false);
        initViews(rootView);
        return rootView;
    }

    private void initViews(View view) {
        mScrollViewPager = view.findViewById(R.id.view_pager_image);
        mRecyclerView = view.findViewById(R.id.news_recycler);
        ll1 = view.findViewById(R.id.ll_1);
        ll2 = view.findViewById(R.id.ll_2);
        ll3 = view.findViewById(R.id.ll_3);
        ll4 = view.findViewById(R.id.ll_4);
        ll1.setOnClickListener(this);
        ll2.setOnClickListener(this);
        ll3.setOnClickListener(this);
        ll4.setOnClickListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        getInfo();

        BaseViewPagerAdapter<String> adapter = new BaseViewPagerAdapter<String>(getActivity(), listener) {
            @Override
            public void loadImage(ImageView imageView, int i, String s) {
                Glide.with(MainFragment.this).load(s).into(imageView);
            }

            @Override
            public void setSubTitle(TextView textView, int i, String s) {
                textView.setText(null);
            }
        };

        mScrollViewPager.setAdapter(adapter);
        adapter.add(initData());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mScrollViewPager.onDestroy();
    }

    private List<String> initData() {
        return new ArrayList<>(Arrays.asList(paths));
    }

    private BaseViewPagerAdapter.OnAutoViewPagerItemClickListener listener = new BaseViewPagerAdapter.OnAutoViewPagerItemClickListener() {
        @Override
        public void onItemClick(int position, Object o) {
            Toast.makeText(getActivity(), "Test", Toast.LENGTH_SHORT).show();
        }
    };

    private void getInfo() {
        RetrofitHelper.getNewsService()
                .getNewsInfo()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<NewsInfoBean>>() {
                    @Override
                    public void onCompleted() {
                        NewsInfoAdapter adapter = new NewsInfoAdapter(dataList);
                        mRecyclerView.setAdapter(adapter);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<NewsInfoBean> newsInfoBeans) {
                        dataList = newsInfoBeans;
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_1:
                Intent intent = new Intent(getActivity(), RedWoodKnowActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_2:
                startActivity(new Intent(getActivity(), BrandInfoActivity.class));
                break;
            case R.id.ll_3:
                startActivity(new Intent(getActivity(), FurnitureBuyActivity.class));
                break;
            case R.id.ll_4:
                startActivity(new Intent(getActivity(), ShowProductsActivity.class));
                break;
        }
    }
}
